<template>
    <div>
        <el-dialog title="新增用户" :visible="true" width="800px" :modal-append-to-body="false" :close-on-click-modal="false"
            @close="handleClose">
            <el-form id="forms" ref="forms" :rules="rulesModel" :model="form" label-width="140px">
                <el-form-item label="手机号" prop="phone">
                    <el-input v-model="form.phone" type="number" placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="登录密码" prop="password">
                    <el-input v-model="form.password" type="text" placeholder="请输入登录密码" />
                </el-form-item>
                <el-form-item label="邀请人手机/邀请码" prop="invPhone">
                    <el-input v-model="form.invPhone" type="text" placeholder="请输入邀请人手机/邀请码" />
                </el-form-item>
                <el-form-item label="姓名" prop="truename">
                    <el-input v-model="form.truename" type="text" placeholder="请输入姓名" />
                </el-form-item>
                <el-form-item label="身份证号" prop="idcard">
                    <el-input v-model="form.idcard" type="text" placeholder="请输入身份证号" />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="handleClose">取 消</el-button>
                <el-button :loading="buttonLoading" type="primary" @click="submit()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
    
<script>
import { Register } from "@/api/users.js";
import { GetEnum } from '@/api/ctrls.js'
import { AESencrypt } from '@/utils/mima'
export default {
    props: {
        info: {
            default: {},
            type: Object
        },
    },
    data() {
        return {
            form: {}, // 详情实体
            rulesModel: {
                phone: [
                    { required: true, message: '请输入手机号', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入登录密码', trigger: 'blur' }
                ],
                invPhone: [
                    { required: true, message: '请输入邀请人手机/邀请码', trigger: 'blur' }
                ],
            },
            buttonLoading: false,

            coinList: {},// 币种枚举
        }
    },

    mounted() {
        this.setForm();
    },
    methods: {
        // 获取枚举
        async GetEnum() {
            const res = await GetEnum();
            this.coinList = res.data['Coin'];
            console.log('币种：：', this.coinList);
            if (this.form.coinid == 0) {
                this.form.coinid = Object.keys(this.coinList)[0]
            }
        },
        /**关闭弹窗 */
        handleClose() {
            this.$parent.modalStatus = 0
        },
        /**表单参数初始化 */
        setForm() {
            this.form = {
                phone: '',
                password: '',
                invPhone: '',
                truename: '',
                idcard: '',
            };
        },
        /**（提交） */
        async submit() {
            try {
                try {
                    await this.$refs['forms'].validate()
                } catch (e) {
                    console.log(e)
                    return
                }
                this.form.password = AESencrypt(this.form.password)
                const { success, msg } = await Register(this.form)
                if (success) {
                    this.handleClose();
                }else{
                    this.form.password = ''
                }
                this.$message({
                    message: msg,
                    type: success ? 'success' : 'error'
                })
            } catch (e) {
                // 取消
                console.log(e)
            }
        },
    }
}
</script>
    
<style lang="scss" scoped>
.grid2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
}
</style>
    
    